﻿using static _01_Tower.Map;
using static System.Net.Mime.MediaTypeNames;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace _01_Tower
{
    class Hero
    {
        Dir dir;
        Dir dir2;
        ConsoleKeyInfo key;
        Map map;
        Information info;
        Game game;
        Monster monster;
        HPBar hpbar;
        static public Pos pos;
        int monNum,tooNum;

        public int atk, def, hp;
        int damage;
        Random rand;

        Input input;

        public struct Pos()
        {
            public int x;
            public int y;
            public int x0;
            public int y0;
        }

        enum Dir
        {
            None,
            Up,
            Down,
            Left,
            Right,
        };

        public Hero()
        {
            
        }

        public void Init(Map map, Information info, Monster monster, Input input,HPBar hpbar)
        {
            this.map = map;
            this.info = info;
            this.monster = monster;
            this.input = input;
            this.hpbar = hpbar;
            //this.game = game;
            pos.x0 = pos.x; 
            pos.y0 = pos.y;
            monNum = tooNum= 1;
            rand = new Random();
            atk = rand.Next(11,20);
            def = 20;
            hp = 20;
            damage = 0;
        }
        void Move()
        {
            
            switch (dir)
            {
                case Dir.Up:
                    pos.y--;
                    break;
                case Dir.Down:
                    pos.y++;
                    break;
                case Dir.Left:
                    pos.x--;
                    break;
                case Dir.Right:
                    pos.x++;
                    break;
            }
        }
        public void Input()
        {
            dir = Dir.None;
            if (input.IsKeyPressed(ConsoleKey.UpArrow) || input.IsKeyPressed(ConsoleKey.W))
            {
                dir = Dir.Up;
                dir2 = Dir.Up;
            }
            if (input.IsKeyPressed(ConsoleKey.DownArrow) || input.IsKeyPressed(ConsoleKey.S))
            {
                dir = Dir.Down;
                dir2 = Dir.Down;
            }
            if (input.IsKeyPressed(ConsoleKey.LeftArrow) || input.IsKeyPressed(ConsoleKey.A))
            {
                dir = Dir.Left;
                dir2 = Dir.Left;
            }
            if (input.IsKeyPressed(ConsoleKey.RightArrow) || input.IsKeyPressed(ConsoleKey.D))
            {
                dir = Dir.Right;
                dir2 = Dir.Right;
            }
        }
        Pos GetNextPos()
        {
            Pos next;
            next.x = next.y = 0;
            next.x0 = next.y0 = 0;
            switch (dir)
            {
                case Dir.Up:
                    next.x = pos.x;
                    next.y = pos.y - 1;
                    break;
                case Dir.Down:
                    next.x = pos.x;
                    next.y = pos.y + 1;
                    break;
                case Dir.Left:
                    next.x = pos.x - 1;
                    next.y = pos.y;
                    break;
                case Dir.Right:
                    next.x = pos.x + 1;
                    next.y = pos.y;
                    break;
            }
            return next;
        }
        int GetMapType(Pos pos)
        {
            return map.map[pos.y, pos.x];
        }
        public void Update()
        {
            if (GetMapType(GetNextPos()) == (int)MapType.None)
            {
                Move();
            }
            else if (GetMapType(GetNextPos()) == (int)MapType.Mon)
            {
                Move();
                Attack(); 
                map.map[pos.y, pos.x] = (int)MapType.None;
                
            }
            else if (GetMapType(GetNextPos()) == (int)MapType.Tool)
            {
                Move();
                info.AddInfo($"获得道具{tooNum++}", ConsoleColor.Yellow);
                map.map[pos.y, pos.x] = (int)MapType.None;
            }
        }
        public void Render()
        {
            Console.SetCursorPosition(pos.x0, pos.y0);
            //Console.WriteLine(" ");
            pos.x0 = pos.x;
            pos.y0 = pos.y;
            Console.SetCursorPosition(pos.x, pos.y);
            if (dir2 == Dir.Up)
            {
                Console.WriteLine("^");
            }
            else if (dir2 == Dir.Down)
            {
                Console.WriteLine("V");
            }
            else if (dir2 == Dir.Left)
            {
                Console.WriteLine("<");
            }
            else if (dir2 == Dir.Right)
            {
                Console.WriteLine(">");
            }

        }
        public void End()
        {

        }
        public bool Attack(bool fight = true)
        {
            while (true)
            {
                if (hp > 0 && monster.hp > 0)
                {
                    info.AddInfo($"你对{monster.name}发起了攻击", ConsoleColor.Blue);
                    
                    if (atk > monster.def)
                    {
                        damage = atk - monster.def;
                        info.AddInfo($"你对{monster.name}造成了{damage}点伤害", ConsoleColor.Blue);
                        
                        monster.hp -= damage;
                        info.AddInfo($"{monster.name}还剩{monster.hp}点血", ConsoleColor.Blue);
                        
                    }
                    else
                    {
                        info.AddInfo($"你对{monster.name}造成了0点伤害", ConsoleColor.Blue);
                        
                    }
                    if (monster.hp > 0 && hp > 0)
                    {
                        info.AddInfo($"怪物{monster.name}向你发起了攻击", ConsoleColor.Red);
                        if (monster.atk > def)
                        {
                            damage = monster.atk - def;
                            info.AddInfo($"你受到了{damage}点伤害", ConsoleColor.Red);
                            hpbar.SetHp(hp-=damage);
                            //hp -= damage;
                            info.AddInfo($"你还剩{hp}点血", ConsoleColor.Red);

                        }
                        else
                        {
                            info.AddInfo($"你受到了0点伤害", ConsoleColor.Red);

                        }
                    }
                }
                
                else if (hp <= 0)
                {
                    info.AddInfo($"你被{monster.name}击败了", ConsoleColor.Green);
                    fight = false;
                    break;
                }
                else if (monster.hp <= 0)
                {
                    info.AddInfo($"你击败了怪物{monster.name}", ConsoleColor.Yellow);
                    fight = true;
                    break;
                }
            }
            return fight;
        }
    }
}
